$(function(){

    //移除占位div
    setTimeout(function(){
        $("#placeholder_div").remove();
    },1000);

    //导航栏的切换
    var flagBefore = 0;
    $("#main_content>ul>li:gt(0)").each(function (index2, element2) {
            $(element2).slideToggle("normal");
    });
    $("#nav_ul>li:gt(1):lt(6)").each(function(index1,element1) {
        //导航栏li被点击
        $(element1).click(function () {
            // alert(index1+"被点击")
            if(index1!=flagBefore){
                $("#main_content>ul>li").each(function (index2, element2) {
                    if (index2 == flagBefore) {
                        // alert("关掉"+index2);
                        $(element2).slideToggle("normal");
                    }
                });
                $("#main_content>ul>li").each(function (index2, element2) {
                    if (index2 == index1) {
                        // alert("打开"+index2);
                        $(element2).slideToggle("normal");
                        flagBefore = index1;
                    }
                })
            }
        })
    });
    
    //AJAX头像图片名字请求
    $.post(
        "/headPortraitServlet",
        {},
        function (data){
            $("#userPortrait").prop("src","../img/userPortrait/"+data+".jpg");
            $("#head_portrait").prop("src","../img/userPortrait/"+data+".jpg");
        },"text"
    );

    function fileContent(s){
        $("#content1_input").css("color","black");
        $("#content1_input").attr("placeholder",s);
        $("#content1_input").removeAttr("disabled","disabled");
    }
    fileContent("无需填写此栏");
    $("#content1_input").attr("disabled","disabled");
    $("#content1_input").css("color","#dddddd");

    //查询员工信息
    //下拉栏选择事件绑定
    layui.use(['layer', 'jquery', 'form'], function () {
        var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form;

        //员工信息查询部分
        form.on('select(content1_select)', function(data){
            if(data.value == 1){
                $("#searchSessionNum").attr("disabled","true");
                form.render('select');
            }else{
                $("#searchSessionNum").removeAttr("disabled");
                form.render('select');//select是固定写法 不是选择器
                //执行开始
                if($("#query_way").val()==="all"){
                    fileContent("无需填写此栏");
                    $("#content1_input").attr("disabled","disabled");
                    $("#content1_input").css("color","#dddddd");
                }
                else if($("#query_way").val()==="accordance_employno"){
                    fileContent("请输入员工号");
                }
                else if($("#query_way").val()==="accordance_departmentno"){
                    fileContent("请输入部门号");
                }
                else{
                    fileContent("请输入小组号");
                }
            }
        });
    });

    //员工信息表格渲染函数
    function Personan_result() {
        layui.use('table', function () {
            var table = layui.table;

            table.render({
                where: {"queryWay":$("#query_way").val(),"queryno":$("#content1_input").val()},
                elem: '#content1_table_inform',
                id: 'content1_table_inform',
                url: 'http://localhost:8080/queryPersonalInformServlet',
                toolbar: true,
                loading: true,
                page: true,
                method: 'post',
                cols: [[
                    {field: 'employno', title: '员工号', fixed: "left", width: 90, sort: true},
                    {field: 'ename', title: '姓名', width: 80, edit: 'text'},
                    {field: 'groupno',title: '组号', width: 90, minWidth: 90, edit: 'text'},
                    {field: 'gender', title: '性别', width: 70, edit: 'text'},
                    {field: 'birthday', title: '生日', hide: true, minWidth: 120, edit: 'text'},
                    {field: 'native_place', title: '籍贯', hide: true, minWidth: 100, edit: 'text'},
                    {field: 'national', title: '民族', hide: true, minWidth: 70, edit: 'text'},
                    {field: 'political', title: '政治面貌', hide: true, minWidth: 120, edit: 'text'},
                    {field: 'health', title: '身体状况', hide: true, minWidth: 70, edit: 'text'},
                    {field: 'id_card', title: '身份证号码', edit: 'text'},
                    {field: 'tel', title: '电话号码', edit: 'text'},
                    {field: 'school_major', title: '学校及专业', width: 200, edit: 'text'},
                    {field: 'degree', title: '学位', minWidth: 70, edit: 'text'},
                ]]
            });
        });
    };

    Personan_result();
    //点击查询按钮后提交查询请求
    $("#button_query").click(function(){

        if($("#content1_input").val().length==0&&$("#query_way").val()!="all")
            layer.tips("这里是空哒","#content1_input",{
                tips:3
            });
        else{
            Personan_result();
        }
    });

    layui.use(['table', 'util'], function(){
        var table = layui.table
        ,util = layui.util;
        //管理员编辑员工信息
        table.on('edit(content1_result_table)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            var employno = obj.data.employno;
            var field = obj.field;
            var value = obj.value;
            var s = "您确定要将工号："+obj.data.employno+"，名字："+obj.data.ename+"，"+obj.field+"\n信息修改为："+obj.value+"吗？";
            layer.confirm(s, {
                btn: ['确定','取消'] //按钮
              }, function(){
                //修改数据库数据
                //编辑员工信息提交AJAX请求
                $.post(
                    "/updatePersonalInformServlet",
                    {"employno": employno,"field": field, "value": value},
                    function (data){
                        if (data=="true")
                            layer.msg('修改成功', {icon: 1,time: 1000});
                        else{
                            layer.msg('修改失败', {icon: 2,time: 1000});
                            table.reload("content1_table_inform", "ename", false);
                        }
                    }
                )
              }, function (){
                //取消修改的数据，进行表格重载，恢复数据
                table.reload("content1_table_inform", "ename", false);
                }
            );
        });

        //双击行事件，弹出个人职位信息
        table.on('rowDouble(content1_result_table)', function(obj){
            // obj.del();
            var employno = obj.data.employno;
            var ename = obj.data.ename;

            //AJAX获取当前员工的 职位信息
            $.post(
                "/employeeCareerServlet",
                {"employno": employno},
                function (data){
                    var department_name = data.department_name;
                    var group_name = data.group_name;
                    var current_position = data.current_position;
                    var working_time = data.working_time;
                    var employment_time = data.employment_time;

                    //打开职位信息窗口
                    var index = layer.open({
                        type: 2, //此处以iframe举例
                        title: employno+" 的职位信息",
                        area: ['600px', '370px'],
                        shade: 0,
                        offset: 'auto',
                        scrollbar: false,
                        maxmin: true,
                        content: ['staffPositions.html', 'yes'],
                        anim: 2,    //弹出动画
                        btn: ['解雇', '关闭'],
                        success: function (layero){
                            var body = layer.getChildFrame('body', index);
                            //向iframe页面中添加信息
                            body.find('#ename').text(ename);
                            body.find("#department_name").text(department_name);
                            body.find("#group_name").text(group_name);
                            body.find("#current_position").text(current_position);
                            body.find("#working_time").text(working_time);
                            body.find("#employment_time").text(employment_time);
                        },
                        yes: function () {
                            var m = layer.confirm('您确定要解雇 '+ename+"-"+employno+' 吗?', {
                                btn: ['确定','取消'], //按钮
                            },function (){
                                //AJAX请求删除员工信息，包括薪资信息和员工个人信息
                                $.post(
                                    "/deletePersonalInformServlet",
                                    {"employno": employno},
                                    function (data){
                                        obj.del();
                                        layer.closeAll();
                                        layer.msg('解雇成功', {icon: 1,time: 1000});
                                        table.reload("content1_table_inform", "ename", false);
                                    }
                                );//AJAX删除信息结束
                            })//确认解雇 结束
                        },//第一个按钮-解雇员工 函数结束
                        btn2: function () {
                            layer.close(index);
                        }
                    })//职位信息窗口结束
                }//AJAX获取员工职位信息回调函数结束
            )//AJAX获取员工职位信息结束
        });//table行的双击事件结束
    })//layui表格加载结束


    //员工信息调动-穿梭框
    layui.use(['layer', 'jquery', 'form', 'transfer'], function(){
        var layer = layui.layer,
            $ = layui.jquery,
            transfer = layui.transfer,
            form = layui.form;
        var shuttle_data;
        var shuttle_right;

        //查询所有小组信息展示在下拉框中
        $.get(
            "/queryAllGroupServlet",
            function (data){
                for (var i = 0; i < data.length; i++) {
                    $("#shuttle_box1").append(data[i].title)
                    $("#shuttle_box2").append(data[i].title);
                }
                //重新渲染表格-下拉框
                form.render();
            },"json"
        );

        //加载穿梭栏
        transfer.render({
            elem: '#employees_mobilize',  //绑定元素
            showSearch: true,
            data: shuttle_data,
            id: 'employees_transfer', //定义索引
            width: 600,
            height: 580,
            title: ["还未选择小组","还未选择小组"],
            text: {
                none: '这里是空哒', //没有数据时的文案
                searchNone: '没有找到匹配的数据哦' //搜索无匹配数据时的文案
            },//  员工调动 反馈数据库操作  左右穿梭数据
            onchange: function (data,index){
                var groupno;
                if (index==0)
                    groupno = $("#shuttle_box2").val();
                else
                    groupno = $("#shuttle_box1").val();
                var v = true;
                for (var i = 0; i < data.length; i++) {
                    $.post(
                        "/updatePersonalInformServlet",
                        {"employno": data[i].value, "field": "groupno", "value":groupno},
                        function (data){
                            if (data=="true")
                                v = v&&true;
                            else{
                                v = v&&false;
                                layer.msg("第"+(i+1)+"个人员调动失败", {icon: 1,time: 1000});
                                return 0;
                            }
                        }
                    );
                }
                if (v)
                    layer.msg("人员调动成功", {icon: 1,time: 1000});
                else
                    layer.msg("人员调动失败", {icon: 1,time: 1000});

            }//穿梭栏数据穿梭结束
        })

        //  函数 - 获取穿梭框的值
        function shuttle_fun(rightdata){
            //获取左侧下拉栏值
            var groupno1 = $("#shuttle_box1").val();
            //获取右侧下拉栏值
            var groupno2 = $("#shuttle_box2").val();

            //获取穿梭栏的值
            $.post(
                "/queryGroupPersonalShuttleServlet",
                {"groupno1": groupno1, "groupno2": groupno2},
                function (data){
                    shuttle_data = data;
                    //获取右侧栏的值
                    $.post(
                        "/queryGroupPersonalShuttleRightServlet",
                        {"groupno2": groupno2},
                        function (data){
                            shuttle_right = data;
                            //重载穿梭栏
                            transfer.reload('employees_transfer', {
                                title: [groupno1, groupno2],
                                data: shuttle_data,
                                value: shuttle_right,
                            });
                        }
                    );
                }
            );
        }

        // 左侧下拉框选中事件
        form.on('select(shuttle_box1_)', function(data) {
            if (data.value == 1) {
                $("#searchSessionNum").attr("disabled", "true");
                form.render('select');
            } else {
                $("#searchSessionNum").removeAttr("disabled");
                form.render('select');//select是固定写法 不是选择器
                //执行开始-加载左侧栏组员数据信息
                shuttle_fun();
            }
        })

        // 右侧下拉框选中事件
        form.on('select(shuttle_box2_)', function(data) {
            if (data.value == 1) {
                $("#searchSessionNum").attr("disabled", "true");
                form.render('select');
            } else {
                $("#searchSessionNum").removeAttr("disabled");
                form.render('select');//select是固定写法 不是选择器
                //执行开始-加载左侧栏组员数据信息
                shuttle_fun();
            }
        })
    });//穿梭框结束

    // 内容3
    //工资录入
    layui.use(['layer', 'form', 'laydate'], function(){
        var layer = layui.layer,
            form = layui.form,
            laydaye = layui.laydate;

        var personData;

        //日期组件
        laydaye.render({
            elem: '#payoff_time' //指定元素
        })

        // 加载员工账号到下拉栏
        $.post(
            "/queryAllPersonServlet",
            function (data){
                personData = data;
                for (var i = 0; i < data.length; i++) {
                    var s = "<option value='"+data[i].employno+"'>"+data[i].employno+"</option>>"
                    $("#content3_select").append(s);
                }
                //重新渲染下拉框
                form.render();
            }
        );

        //弹出身份验证界面
        function authenticate(){
            var employno =  $("#content3_select").val();
            $.post(
                "/employeeCareerServlet",
                {"employno": employno},
                function (data) {
                    var department_name = data.department_name;
                    var group_name = data.group_name;
                    var current_position = data.current_position;
                    var working_time = data.working_time;
                    var employment_time = data.employment_time;

                    var index = layer.open({
                        type: 2, //此处以iframe举例
                        title: "请仔细确认是否为 "+employno+" 录入工资信息",
                        area: ['600px', '370px'],
                        shade: 0,
                        offset: 'auto',
                        scrollbar: false,
                        maxmin: true,
                        content: ['staffPositions.html', 'yes'],
                        anim: 2,    //弹出动画
                        btn: ['确认', '关闭'],
                        shade: 0.2,
                        success: function (layero) {
                            var body = layer.getChildFrame('body', index);
                            //向iframe页面中添加信息
                            body.find('#ename').text(ename);
                            body.find("#department_name").text(department_name);
                            body.find("#group_name").text(group_name);
                            body.find("#current_position").text(current_position);
                            body.find("#working_time").text(working_time);
                            body.find("#employment_time").text(employment_time);
                        }
                    })
                }
            )
        }

        var ename;
        //选择工号自动填充名字以便核对
        form.on('select(content3_select_filter)', function(data) {
            if (data.value == 1) {
                $("#searchSessionNum").attr("disabled", "true");
                form.render('select');
            } else {
                $("#searchSessionNum").removeAttr("disabled");
                form.render('select');//select是固定写法 不是选择器
                //执行开始
                var employno = $("#content3_select").val();
                for (var i = 0; i < personData.length; i++) {
                    if (employno==personData[i].employno){
                        ename = personData[i].ename;
                        $("#content3_ename").val(ename);
                        authenticate();
                        return 0;
                    }
                }
            }
        });

        function informComplete(){
            if ($("#base_salary").val().length0=0)
                return false;
            if ($("#working_time").val().length==0)
                return false;
            if ($("#bonus").val().length==0)
                return false;
            if ($("#subsidies").val().length==0)
                return false;
            if ($("#overtime_salary").val().length==0)
                return false;
            if ($("#leave_late").val().length==0)
                return false;
            if ($("#tax").val().length==0)
                return false;
            if ($("#payoff_time").val().length==0)
                return false;
            return true;
        }

        $("#entry_button").click(function () {
            if (informComplete()){
                $.post(
                    "/entrySalaryServlet",
                    {"employno": $("#content3_select").val(),
                        "base_salary": $("#base_salary").val(),
                        "working_time": $("#working_time").val(),
                        "bonus": $("#bonus").val(),
                        "subsidies": $("#subsidies").val(),
                        "overtime_salary": $("#overtime_salary").val(),
                        "leave_late": $("#leave_late").val(),
                        "tax": $("#tax").val(),
                        "payoff_time": $("#payoff_time").val()
                    },
                    function (data){
                        if (data=="true")
                            layer.msg('录入薪资成功');
                        else
                            layer.msg('录入薪资失败');
                    }
                );
            }
            else{
                layer.msg('您还未全部录入!', function(){});
            }
        })
    });
})//入口函数结束